package com.xbongbong.saas.script.tempenum;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.PatternEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.SubFormPoJo;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.CustomerExpressEnum;
import com.xbongbong.saas.enums.CustomerInvoiceEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.PayRelationshipEnum;
import com.xbongbong.saas.enums.business.PurchaseInvoiceEnum;
import com.xbongbong.saas.enums.dictionary.HasRedEnum;
import com.xbongbong.saas.enums.dictionary.InvoiceTypeEnum;
import com.xbongbong.saas.enums.dictionary.IsCancelEnum;
import com.xbongbong.saas.enums.dictionary.IsRedEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Getter
public enum TempPurchaseInvoiceEnum {
    //刷脚本专用枚举
    BASE_INFO(PurchaseInvoiceEnum.BASE_INFO),
    SHEET_NO(PurchaseInvoiceEnum.SHEET_NO),
    INVOICE_NUMBER(PurchaseInvoiceEnum.INVOICE_NUMBER),
    APPLY_USER(PurchaseInvoiceEnum.APPLY_USER),
    SUPPLIER_NAME(PurchaseInvoiceEnum.SUPPLIER_NAME),
    PURCHASE_ID(PurchaseInvoiceEnum.PURCHASE_ID),
    PAY_RELATIONSHIP(PurchaseInvoiceEnum.PAY_RELATIONSHIP),
    LINK_PAY_PLAN(PurchaseInvoiceEnum.PAY_PLAN_ID),
    LINK_PAY_SHEET(PurchaseInvoiceEnum.PAY_SHEET_ID),
    INVOICE_DATE(PurchaseInvoiceEnum.INVOICE_DATE),
    TOTAL_MONEY(PurchaseInvoiceEnum.TOTAL_MONEY),
    EXTEND_INFO(PurchaseInvoiceEnum.EXTEND_INFO),
    INVOICE_TYPE(PurchaseInvoiceEnum.INVOICE_TYPE),
    MEMO(PurchaseInvoiceEnum.MEMO),
    ATTACHMENTS(PurchaseInvoiceEnum.ATTACHMENTS),
    SUPPLIER_NAME_LINK_TEXT(PurchaseInvoiceEnum.SUPPLIER_NAME_LINK_TEXT),
    PURCHASE_ID_LINK_TEXT(PurchaseInvoiceEnum.PURCHASE_ID_LINK_TEXT),
    PAY_PLAN_ID_LINK_TEXT(PurchaseInvoiceEnum.PAY_PLAN_ID_LINK_TEXT),
    PAY_SHEET_ID_LINK_TEXT(PurchaseInvoiceEnum.PAY_SHEET_ID_LINK_TEXT),
    IS_RED(PurchaseInvoiceEnum.IS_RED),
    ORIGIN_ID(PurchaseInvoiceEnum.ORIGIN_ID),
    IS_CANCEL(PurchaseInvoiceEnum.IS_CANCEL),
    CUSTOMER_INVOICE(PurchaseInvoiceEnum.CUSTOMER_INVOICE),
    CUSTOMER_EXPRESS(PurchaseInvoiceEnum.CUSTOMER_EXPRESS),
    ORIGIN_ID_LINK_TEXT(PurchaseInvoiceEnum.ORIGIN_ID_LINK_TEXT),
    HAS_RED(PurchaseInvoiceEnum.HAS_RED),
    SYSTEM_INFO(PurchaseInvoiceEnum.SYSTEM_INFO),
    CREATOR_ID(PurchaseInvoiceEnum.CREATOR_ID),
    ADD_TIME(PurchaseInvoiceEnum.ADD_TIME),
    UPDATE_TIME(PurchaseInvoiceEnum.UPDATE_TIME);
    private PurchaseInvoiceEnum anEnum;

    TempPurchaseInvoiceEnum(PurchaseInvoiceEnum anEnum) {
        this.anEnum = anEnum;
    }

    public static List<TempPurchaseInvoiceEnum> getAllEnum() {
        List<TempPurchaseInvoiceEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }

    public static void initInvoiceDate(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setDateType(DateTimeEnum.TIME.getType());
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType("today");
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化客户发票信息
     *
     * @param fieldAttrEntity 实体
     */
    public static void initCustomerInvoice(FieldAttrEntity fieldAttrEntity) {
        SubFormPoJo subFormPoJo = new SubFormPoJo();
        List<FieldAttrEntity> items = new ArrayList<>();
        FieldAttrEntity item = InitExplainsHelper.setField(CustomerInvoiceEnum.TITLE_TYPE.getAttr(), CustomerInvoiceEnum.TITLE_TYPE.getName(), CustomerInvoiceEnum.TITLE_TYPE.getSaasAttr(), FieldTypeEnum.COMBO.getType(), 4, ShowTypeEnum.EDIT_DETAIL.getCode(), 0, 1);
        String str = "企业,个人";
        item.setItems(InitExplainsHelper.setCombItem(str));
        item.setParentAttr(CUSTOMER_INVOICE.anEnum.getAttr());
        items.add(item);
        FieldAttrEntity item2 = InitExplainsHelper.setField(CustomerInvoiceEnum.TITLE.getAttr(), CustomerInvoiceEnum.TITLE.getName(), CustomerInvoiceEnum.TITLE.getSaasAttr(), FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 1);
        item2.setParentAttr(CUSTOMER_INVOICE.anEnum.getAttr());
        items.add(item2);
        FieldAttrEntity item3 = InitExplainsHelper.setField(CustomerInvoiceEnum.TAXPAYER_NO.getAttr(), CustomerInvoiceEnum.TAXPAYER_NO.getName(), CustomerInvoiceEnum.TAXPAYER_NO.getSaasAttr(), FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.EDIT_DETAIL.getCode(), 0, 1);
        item3.setParentAttr(CUSTOMER_INVOICE.anEnum.getAttr());
        items.add(item3);
        FieldAttrEntity item4 = InitExplainsHelper.setField(CustomerInvoiceEnum.REGISTERED_PHONE.getAttr(), CustomerInvoiceEnum.REGISTERED_PHONE.getName(), CustomerInvoiceEnum.REGISTERED_PHONE.getSaasAttr(), FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.EDIT_DETAIL.getCode(), 0, 1);
        item4.setParentAttr(CUSTOMER_INVOICE.anEnum.getAttr());
        items.add(item4);
        FieldAttrEntity item5 = InitExplainsHelper.setField(CustomerInvoiceEnum.INVOICE_ADDRESS.getAttr(), CustomerInvoiceEnum.INVOICE_ADDRESS.getName(), CustomerInvoiceEnum.INVOICE_ADDRESS.getSaasAttr(), FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.EDIT_DETAIL.getCode(), 0, 1);
        item5.setParentAttr(CUSTOMER_INVOICE.anEnum.getAttr());
        items.add(item5);
        FieldAttrEntity item6 = InitExplainsHelper.setField(CustomerInvoiceEnum.BANK.getAttr(), CustomerInvoiceEnum.BANK.getName(), CustomerInvoiceEnum.BANK.getSaasAttr(), FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.EDIT_DETAIL.getCode(), 0, 1);
        item6.setParentAttr(CUSTOMER_INVOICE.anEnum.getAttr());
        items.add(item6);
        FieldAttrEntity item7 = InitExplainsHelper.setField(CustomerInvoiceEnum.ACCOUNT.getAttr(), CustomerInvoiceEnum.ACCOUNT.getName(), CustomerInvoiceEnum.ACCOUNT.getSaasAttr(), FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.EDIT_DETAIL.getCode(), 0, 1);
        item7.setParentAttr(CUSTOMER_INVOICE.anEnum.getAttr());
        items.add(item7);
        subFormPoJo.setItems(items);
        fieldAttrEntity.setSubForm(subFormPoJo);
        /*SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setRelyAttr(SUPPLIER_NAME.anEnum.getAttr());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);*/
        fieldAttrEntity.setShowAttrPermission(BasicConstant.ONE);
    }
    /**
     * 初始化客户寄送地址
     *
     * @param fieldAttrEntity 实体
     */
    public static void initCustomerExpress(FieldAttrEntity fieldAttrEntity) {
        SubFormPoJo subFormPoJo = new SubFormPoJo();
        List<FieldAttrEntity> items = new ArrayList<>();
        FieldAttrEntity item = InitExplainsHelper.setField(CustomerExpressEnum.NAME.getAttr(), CustomerExpressEnum.NAME.getName(), CustomerExpressEnum.NAME.getSaasAttr(), FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.EDIT_DETAIL.getCode(), 0, 1);
        item.setParentAttr(CUSTOMER_EXPRESS.anEnum.getAttr());
        items.add(item);
        FieldAttrEntity item2 = InitExplainsHelper.setField(CustomerExpressEnum.CONTACT.getAttr(), CustomerExpressEnum.CONTACT.getName(), CustomerExpressEnum.CONTACT.getSaasAttr(), FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 1);
        item2.setParentAttr(CUSTOMER_EXPRESS.anEnum.getAttr());
        items.add(item2);
        FieldAttrEntity item3 = InitExplainsHelper.setField(CustomerExpressEnum.PHONE.getAttr(), CustomerExpressEnum.PHONE.getName(), CustomerExpressEnum.PHONE.getSaasAttr(), FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.EDIT_DETAIL.getCode(), 0, 1);
        item3.setParentAttr(CUSTOMER_EXPRESS.anEnum.getAttr());
        items.add(item3);
        FieldAttrEntity item4 = InitExplainsHelper.setField(CustomerExpressEnum.ZIP_CODE.getAttr(), CustomerExpressEnum.ZIP_CODE.getName(), CustomerExpressEnum.ZIP_CODE.getSaasAttr(), FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.EDIT_DETAIL.getCode(), 0, 1);
        item4.setParentAttr(CUSTOMER_EXPRESS.anEnum.getAttr());
        item4.setPatternType(PatternEnum.POSTCODE.getType());
        item4.setVerifyRule(PatternEnum.POSTCODE.getPattern());
        item4.setVerifyMessage(PatternEnum.POSTCODE.getMsg());
        items.add(item4);
        FieldAttrEntity item5 = InitExplainsHelper.setField(CustomerExpressEnum.ADDRESS.getAttr(), CustomerExpressEnum.ADDRESS.getName(), CustomerExpressEnum.ADDRESS.getSaasAttr(), FieldTypeEnum.ADDRESS.getType(), 4, ShowTypeEnum.EDIT_DETAIL.getCode(), 0, 1);
        item5.setParentAttr(CUSTOMER_EXPRESS.anEnum.getAttr());
        items.add(item5);
        subFormPoJo.setItems(items);
        fieldAttrEntity.setSubForm(subFormPoJo);
       /*SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setRelyAttr(SUPPLIER_NAME.anEnum.getAttr());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);*/
        fieldAttrEntity.setShowAttrPermission(BasicConstant.ONE);
    }

    /**
     * 初始化发票编号流水号
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     * @author feng.zheng
     * @date 2019/1/18 14:09
     */
    public static void initSheetNo(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.PURCHASE_INVOICE.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.PURCHASE_INVOICE.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(SHEET_NO.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setSerialNumber(InitExplainsHelper.setSerialNumber(SerialNumberEnum.PURCHASE_INVOICE_NO));
        fieldAttrEntity.setNoRepeat(1);
        fieldAttrEntity.setIsForbidden(0);
    }
    /**
     * 初始化发票类型
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     * @author feng.zheng
     * @date 2019/1/18 14:12
     */
    public static void initInvoiceType(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(InvoiceTypeEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setDictionaryFlag(1);
    }

    public static void initIsRed(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(IsRedEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setDictionaryFlag(0);
    }

    public static void initHasRed(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(HasRedEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setDictionaryFlag(0);
    }

    public static void initIsCancel(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(IsCancelEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setDictionaryFlag(0);
    }

    /**
     * 初始化关联客户
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initSupplierName(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.SUPPLIER.getCode());
        fieldAttrEntity.setLinkedText(SUPPLIER_NAME_LINK_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化关联原始发票
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initOriginId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.PURCHASE_INVOICE.getCode());
        fieldAttrEntity.setLinkedText(ORIGIN_ID_LINK_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }


    /**
     * 初始化关联采购合同
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initPurchaseId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.PURCHASE.getCode());
        fieldAttrEntity.setLinkedText(PURCHASE_ID_LINK_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(true);
        saasParticularAttributePoJo.setRelyAttr(SUPPLIER_NAME.anEnum.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.SUPPLIER.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initPayRelationship(FieldAttrEntity fieldAttrEntity){
        List<ItemPoJo> itemPoJos = new ArrayList<>();
        for(PayRelationshipEnum payRelationshipEnum : PayRelationshipEnum.values()){
            if (Objects.equals(payRelationshipEnum.getCode(),PayRelationshipEnum.UNKNOWN.getCode())){
                continue;
            }
            ItemPoJo itemPoJo = new ItemPoJo();
            itemPoJo.setText(I18nMessageUtil.getMessage(payRelationshipEnum.getAlias()));
            itemPoJo.setValue(payRelationshipEnum.getCode());
            itemPoJo.setShowType(ShowTypeEnum.ALL.getCode());
            List<String> fieldMap = new ArrayList<>();
            if(payRelationshipEnum == PayRelationshipEnum.PAY_PLAN){
                itemPoJo.setChecked(true);
                fieldMap.add(LINK_PAY_PLAN.anEnum.getAttr());
            }else if(payRelationshipEnum == PayRelationshipEnum.PAY_SHEET){
                itemPoJo.setChecked(false);
                fieldMap.add(LINK_PAY_SHEET.anEnum.getAttr());
            }
            itemPoJo.setNoEdit(BasicConstant.ONE);
            itemPoJo.setNoDel(BasicConstant.ONE);
            itemPoJo.setFieldMap(fieldMap);
            itemPoJos.add(itemPoJo);
        }
        fieldAttrEntity.setItems(itemPoJos);
        fieldAttrEntity.setShowType(ShowTypeEnum.ALL.getCode());
        fieldAttrEntity.setShowAttrPermission(BasicConstant.ONE);
        fieldAttrEntity.setNoItemLink(BasicConstant.ONE);
        fieldAttrEntity.setIsForbidden(1);
    }
    /**
     * 初始化关联付款计划
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initPayPlanId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.PAY_PLAN.getCode());
        fieldAttrEntity.setLinkedText(PAY_PLAN_ID_LINK_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(true);
        saasParticularAttributePoJo.setRelyAttr(PURCHASE_ID.anEnum.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.PURCHASE.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(BasicConstant.ZERO);
        fieldAttrEntity.setShowAttrPermission(BasicConstant.ZERO);
    }
    /**
     * 初始化关联回款单
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initPaySheetId(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.PAY_SHEET.getCode());
        fieldAttrEntity.setLinkedText(PAY_SHEET_ID_LINK_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(true);
        JSONArray array = new JSONArray();
        JSONObject relySupplier = new JSONObject();
        relySupplier.put(StringConstant.ATTR, PurchaseInvoiceEnum.SUPPLIER_NAME.getAttr());
        relySupplier.put(StringConstant.BUSINESS_TYPE, XbbRefTypeEnum.SUPPLIER.getCode());
        relySupplier.put("priority", 1);
        JSONObject relyPurchase = new JSONObject();
        relyPurchase.put(StringConstant.ATTR, PurchaseInvoiceEnum.PURCHASE_ID.getAttr());
        relyPurchase.put(StringConstant.BUSINESS_TYPE, XbbRefTypeEnum.PURCHASE.getCode());
        relyPurchase.put("priority", 2);
        array.add(relyPurchase);
        array.add(relySupplier);
        saasParticularAttributePoJo.setRelyArray(array);
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(BasicConstant.ZERO);
        fieldAttrEntity.setShowAttrPermission(BasicConstant.ZERO);
    }

    public static void initTotalMoney(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(1);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    /**
     * 初始化申请人
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initApplyUser(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setIsForbidden(0);
    }
}
